/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.museum.main;

import java.net.URI;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.museum.utils.Utils;

public class DigMoney {
	String[] users = new String[] { "chinesegxf", "chinesegxf0", "chinesegxf1",
			"chinesegxf2", "chinesegxf3", "chinesegxf4", "chinesegxf5",
			"chinesegxf6", "chinesegxf7", "chinesegxf8", "chinesegxf9",
			"chinesegxfa", "chinesegxfb", "chinesegxfc", "chinesegxfd",
			"chinesegxfe", "chinesegxff", "chinesegxfg", "chinesegxfh",
			"chinesegxfi", "chinesegxfj", "chinesegxfk" };
	String[] uids = new String[] { "4209691217", "6334744444", "6307237098",
			"6312834312", "6321922907", "6322202244", "6322237435",
			"6322255972", "6322273011", "6322291954", "6322303718",
			"6322331823", "6322399814", "6322427366", "6359906517",
			"7079378127", "7089234879", "7093492841", "7101333054",
			"7122231616", "7128634655","7145521224" };
	String[] cookies = new String[] {
			"MY:SESSION=4e8025b303teliiha73008eb2284e0b3; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=4209691217; MY:SNID=9471d911b04f701f4f3f19151e814da6",
			"MY:SESSION=4e81585f05sp91rsc0b69498deb0aa91; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6334744444; MY:SNID=5780dcc9182e5883d8fb4cff54406ba4",
			"MY:SESSION=4e80213705rv1j7ad4dc166a75243b3c; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6307237098; MY:SNID=a667e9db2fc15f1080984e97f1a9a83e",
			"MY:SESSION=4e80253d05s4cd88872cb05f990d6a9e; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6312834312; MY:SNID=1c0d1c7567d1432d1415c53c10d9877b",
			"MY:SESSION=4e8131c705sd1oqr41197d8012852ebc; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6321922907; MY:SNID=48ff481739e01e0e1ba8cb8e1be46e75",
			"MY:SESSION=4e81325505sda9k42fd973832449039e; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6322202244; MY:SNID=ac6d3e85c409c5c1624f28ba86f3349b",
			"MY:SESSION=4e8133f505sdbbvr4bfce91d6d87cc27; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6322237435; MY:SNID=321365afbfcef02d5673519b7eda97fb",
			"MY:SESSION=4e8133f505sdbu341492c690faf28eb1; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6322255972; MY:SNID=6439c2abebfa752f99bfaab4b14d8248",
			"MY:SESSION=4e81348305sdcenjc1a5d780a96c6985; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6322273011; MY:SNID=107cb995b79b82b96e9aa67b4d454aff",
			"MY:SESSION=4e8134c005sdd17i855337c4455e3705; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6322291954; MY:SNID=fbc365a255ed6b258a34d300a0eb621d",
			"MY:SESSION=4e8134fe05sddcn6b5d75a801d53eec3; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6322303718; MY:SNID=e43bd2c226387a097771e2a63a2d55c9",
			"MY:SESSION=4e81353305sde85f67127894b8de570f; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6322331823; MY:SNID=6d206ce50724abb8a058ec835c57cb80",
			"MY:SESSION=4e81356705sdgai699af84ffbf7ffa48; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6322399814; MY:SNID=13e5db20418e7a0bb8938a0842004c07",
			"MY:SESSION=4e81359705sdh5f6c17fb19a5b16f2aa; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6322427366; MY:SNID=8ea90bb1fc8401693ca1dd844be756fa",
			"MY:SESSION=4e827a0e05th8u6l33ed7f0cc68614f0; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F; city_id=0; city_old_id=0; MY:UserID=6359906517; MY:SNID=60d4fd82f1632826b49b47176d773b80",
			"MY:UserID=7079378127; MY:SNID=e9699808f371016b2ec7215db1ab3aba; MY:SESSION=4eb29ad406ivdf6fcff23e0c4248f97e; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F",
			"MY:UserID=7089234879; MY:SNID=b0d7d23a3a9e2874f7c12b514fd604cd; MY:SESSION=4eb3c6d106j8q8tv2212809a2bde0e5d; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F",
			"MY:UserID=7093492841; MY:SNID=72a9f065966ebe15b7492c2dd58fd6da; MY:SESSION=4eb3c90106jcs7398fbcc286a6a8fd84; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F",
			"MY:UserID=7101333054; MY:SNID=32d768e1c8c384a3ee5195da090994e4; MY:SESSION=4eb483b706jkbfhu9d22c11dede9c9fe; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F",
			"MY:UserID=7122231616; MY:SNID=54f7c1030aa96958172bf758076ed1a5; MY:SESSION=4eb7dc7506k898a02b8dc93df99caecb; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F",
			"MY:UserID=7128634655; MY:SNID=c843fe982ae304d0825ab737ef5e2cd3; MY:SESSION=4eb7dcc206kecl8v6dcf95489b7a2927; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F",
			"MY:UserID=7145521224; MY:SNID=ab227ba4199d90a9e68617ee894a6a5a; MY:SESSION=4eb7de9606kug028026f1f32d0cf3403; MY:my_sig_prefix=http%3A%2F%2Fwww.btbird.com%2F", };

	int delay = 2000 / users.length;
	DefaultHttpClient hc = new DefaultHttpClient();

	public void dig() throws Exception {
		HttpPost post = Utils.createPost();
		for (int i = 0; i < 4860; i++) {
			for (int j = 0; j < uids.length; j++) {
				try {
					Thread.sleep(delay);
					String url = "http://my.mowhere.com/interface/wa.php";
					post.setURI(new URI(url));
					post.setHeader("Cookie", cookies[j]);
					post.setEntity(Utils.genWaEntity(uids[j], "7", "10"));
					String resjson = Utils.trans(hc.execute(post));
					JsonObject js = new JsonParser().parse(resjson)
							.getAsJsonObject();
					String err = js.get("err").getAsString();
					if ("体力不足".equals(err)) {
						sell(post, j);
						buy(post, j);
						use(post, j);
					} else if ("".equals(err)) {
						JsonObject data = js.getAsJsonObject("data");
						if (data.has("err")) {
							String dataerr = data.get("err").getAsString();
							if ("由于您的储物间已满，所以此宝物自动丢弃！<BR>".equals(dataerr)) {
								sell(post, j);
							}
						}
						if (data.has("treasuretext")) {
							String r = data.get("treasuretext").getAsString();
							System.out.println(i + "--" + users[j] + "==" + r);
						} else {
							System.out.println(i + "--" + users[j] + "==挖光了!");
						}
					} else {
						System.out.println(i + "--" + users[j] + "==" + err);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void buy(HttpPost post, int uidx) throws Exception {
		String url = "http://my.mowhere.com/interface/manage_treasure.php";
		post.setURI(new URI(url));
		post.setHeader("Cookie", cookies[uidx]);
		post.setEntity(Utils.genBuyEntity(uids[uidx], "402"));
		String err = "1";
		while (!"".equals(err)) {
			Thread.sleep(2000);
			String resjson = Utils.trans(hc.execute(post));
			JsonObject js = new JsonParser().parse(resjson).getAsJsonObject();
			err = js.get("err").getAsString();
			JsonObject data = js.getAsJsonObject("data");
			String r = data.get("text").getAsString();
			System.out.println(users[uidx] + "###" + r);
		}
	}

	public void use(HttpPost post, int uidx) throws Exception {
		String url = "http://my.mowhere.com/interface/manage_treasure.php";
		post.setURI(new URI(url));
		post.setHeader("Cookie", cookies[uidx]);
		post.setEntity(Utils.genUseEntity(uids[uidx], "402"));
		String err = "1";
		while (!"".equals(err)) {
			Thread.sleep(2000);
			String resjson = Utils.trans(hc.execute(post));
			JsonObject js = new JsonParser().parse(resjson).getAsJsonObject();
			err = js.get("err").getAsString();
			JsonObject data = js.getAsJsonObject("data");
			String r = data.get("text").getAsString();
			System.out.println(users[uidx] + "###" + r);
		}
	}

	public void sell(HttpPost post, int uidx) throws Exception {
		String url = "http://my.mowhere.com/interface/manage_treasure.php";
		post.setURI(new URI(url));
		post.setHeader("Cookie", cookies[uidx]);
		post.setEntity(Utils.genSellEntity(uids[uidx], "all"));
		String err = "1";
		while (!"".equals(err)) {
			Thread.sleep(2000);
			String resjson = Utils.trans(hc.execute(post));
			JsonObject js = new JsonParser().parse(resjson).getAsJsonObject();
			err = js.get("err").getAsString();
			JsonObject data = js.getAsJsonObject("data");
			String r = data.get("text").getAsString();
			System.out.println(users[uidx] + "###" + r);
		}
	}

	public static void main(String[] args) throws Exception {
		DigMoney r = new DigMoney();
		r.dig();
	}
}
